<?php

class TrackDAO extends DAO {
    public function select($fields='*', $condition='TRUE', $page='1', $rows_per_page='20', $order_by='id_cuenta', $order_by_type='DESC') {
        $list = array();
        $data = array();
        try {
//            $rows = 0;
//            $start = intval(($page-1)*$rows_per_page);
//            $query = "SELECT COUNT(id_track) AS rows FROM track";
//            $query .= " WHERE ".$condition;
//            $stmt = $this->dbh->query($query);
//            $rows = $stmt->fetchObject()->rows;
            $query = 'SELECT '. $fields .' FROM track';
            $query .= ' WHERE '.$condition.' ';
            //$pages = ceil($rows/$rows_per_page);
            $query .= 'ORDER BY :order_by';
            $query .= ' :order_by_type ';
            //echo $query;
//            $query .= 'LIMIT :start, :rows_per_page';
//            $stmt->closeCursor();
            $stmt = $this->dbh->prepare($query);
//            $stmt->bindParam(':start', $start, PDO::PARAM_INT);
//            $stmt->bindParam(':rows_per_page', $rows_per_page, PDO::PARAM_INT);
            $stmt->bindParam(':order_by', $order_by);
            $stmt->bindParam(':order_by_type', $order_by_type);
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            $stmt->execute();
            $result = $stmt->fetchAll();
            foreach($result as $row) {
                $objTrackTo = new TrackTO();
                $objTrackTo->setIdTrack($row->id_track);
                $objTrackTo->setNombre($row->nombre);
                $objTrackTo->setDescripcion($row->descripcion);
                $objTrackTo->setIdChair($row->id_chair);
                $list[count($list)+1] = $objTrackTo;
            }
            $data['list']=$list;
//            $data['pages']=$pages;
//            $data['rows']=$rows;

            return $data;

        }catch(Exception $e) {
            throw $e;
        }
    }

    public function load($objTrackTO) {
        try {
            $query = 'SELECT * FROM track WHERE id_track = :id_track LIMIT 1';
            $stmt = $this->dbh->prepare($query);
            $stmt->bindParam(':id_track', $objTrackTO->getIdTrack());
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            $stmt->execute();
            $resultado = $stmt->fetch();
            $objTrackTO->setNombre($resultado->nombre);
            return $objTrackTO;
        }
        catch(Exception $e) {
            throw $e;
        }
    }

    public function findTrackByIdChair($id_chair) {
        try {
            $query = 'SELECT * FROM track WHERE id_chair = :id_chair';
            $stmt = $this->dbh->prepare($query);
            $stmt->bindParam(':id_chair', $id_chair);
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            $stmt->execute();
            $resultado = $stmt->fetch();
            $id_track = $resultado->id_track;
            return $id_track;
        }
        catch(Exception $e) {
            throw $e;
        }
    }

    public function delete($id) {
        return "";
    }

    protected function update($objetoTO) {
        return "";
    }

    protected function insert($objetoTO) {
        return "";
    }
}
?>
